package org.Big;

//import java.math.RoundingMode;
import java.math.BigDecimal;

import java.math.RoundingMode;
import java.util.Arrays;

public class TestDecimal {
    public static void test() {
        BigDecimal bd = new BigDecimal("123.4567");
        System.out.println(bd.multiply(bd)); // 15241.55677489

        BigDecimal d1 = new BigDecimal("123.45");
        BigDecimal d2 = new BigDecimal("123.4500");
        BigDecimal d3 = new BigDecimal("1234500");
        System.out.println(d1.scale()); // 2,两位小数
        System.out.println(d2.scale()); // 4
        System.out.println(d3.scale()); // 0

        BigDecimal d21 = new BigDecimal("123.4500");
        BigDecimal d22 = d21.stripTrailingZeros();
        System.out.println(d21.scale()); // 4
        System.out.println(d22.scale()); // 2,因为去掉了00

        BigDecimal d33 = new BigDecimal("1234500");
        BigDecimal d4 = d33.stripTrailingZeros();
        System.out.println(d33.scale()); // 0
        System.out.println(d4.scale()); // -2

        System.out.println("value = 1234500 " + Arrays.toString("1234500".toCharArray()));

        d1 = new BigDecimal("123.456789");
        d2 = d1.setScale(4, RoundingMode.HALF_DOWN); // 四舍五入，123.4568
        d3 = d1.setScale(4, RoundingMode.DOWN); // 直接截断，123.4567
        System.out.println(d2);
        System.out.println(d3);

        d1 = new BigDecimal("123.456");
        d2 = new BigDecimal("23.456789");
        d3 = d1.divide(d2, 10, RoundingMode.HALF_UP);// BigDecimal.ROUND_HALF_UP); // 保留10位小数并四舍五入
        try {
            d4 = d1.divide(d2); // 报错：ArithmeticException，因为除不尽
        } catch (ArithmeticException exception) {
            System.out.println("divide exception = " + exception.toString());
        }


        System.out.println(d3);
        System.out.println(d4);

        BigDecimal n = new BigDecimal("12.345");
        BigDecimal m = new BigDecimal("0.12");
        BigDecimal[] dr = n.divideAndRemainder(m);
        System.out.println(dr[0]); // 102
        System.out.println(dr[1]); // 0.105
    }
}
